package com.zxgj.datadirect.util;

import com.zxgj.datadirect.entity.DataDirectCatalog;
import com.zxgj.datadirect.mapper.DataDirectCatalogMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Slf4j
@Component
public class TestDataGenerator {
    
    @Autowired
    private DataDirectCatalogMapper catalogMapper;
    
    private static final Random random = new Random();
    
    public void generateTestData() {
        List<DataDirectCatalog> catalogs = new ArrayList<>();
        
        // 生成测试数据
        for (int i = 1; i <= 10; i++) {
            DataDirectCatalog catalog = new DataDirectCatalog();
            catalog.setCatalogCode("CAT" + String.format("%03d", i));
            catalog.setCatalogName(getRandomCatalogName());
            catalog.setOrgCode("ORG" + String.format("%03d", random.nextInt(5) + 1));
            catalog.setDataType(random.nextInt(5) + 1);
            
            catalogs.add(catalog);
        }
        
        // 批量插入
        for (DataDirectCatalog catalog : catalogs) {
            try {
                if (catalogMapper.existsByCatalogCode(catalog.getCatalogCode()) == 0) {
                    catalogMapper.insert(catalog);
                }
            } catch (Exception e) {
                log.error("插入数据失败: {}", e.getMessage());
            }
        }
    }
    
    private String getRandomCatalogName() {
        String[] names = {"基础信息", "业务数据", "统计报表", "分析报告", "监测数据"};
        return names[random.nextInt(names.length)] + "集";
    }
}
